package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/employee")
@Controller
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IDepartmentService iDepartmentService;

    @Autowired
    private IRoleService iRoleService;

//    分页过滤查询
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject em){
//        int i=1/0;
        model.addAttribute("pageInfo",iEmployeeService.que2(em));
        model.addAttribute("departments",iDepartmentService.selectAll());
        return "/employee/list";
    }
//    删除
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            iEmployeeService.deleteByPrimaryKey(id);
            iEmployeeService.deleteByRoleId(id);
        }
        return "redirect:/employee/list";
    }
// 去到编辑或新增界面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        List<Department> departments = iDepartmentService.selectAll();
        model.addAttribute("departments",departments);
//        角色
        model.addAttribute("roles",iRoleService.selectAll());
//        编辑时候的角色回显
        model.addAttribute("selfRoles",iRoleService.selectByEmid(id));

        if (id!=null){
            model.addAttribute("employee",iEmployeeService.selectByPrimaryKey(id));
        }
        return "employee/input";
    }
//    新增保存或编辑保存
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId()!=null){
            iEmployeeService.updateByPrimaryKey(employee);
        }else {
            iEmployeeService.insert(employee);
        }
//需要上面的新增操作执行完,才能有对应的员工的id
        for (Long id:ids){
            iEmployeeService.insertByRoleId(employee.getId(),id);
        }

        return "redirect:/employee/list";
    }

//    导出表
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
//        解决导出的是中文名的问题
        String filename = new String("员工.xls".getBytes("UTF-8"), "ISO-8859-1");
//      导出的时候需要设置响应头
        response.setHeader("Content-Disposition","attachment;filename="+filename);
//        把表响应到浏览器
          iEmployeeService.exprotXls(response.getOutputStream());
    }

//    导入表
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
//        需要配置文件上传的依赖,和在MVC层设置上传文件的大小, file必须跟那边的参数名一致
         iEmployeeService.importXls(file);
        return "redirect:/employee/list";
    }
}
